package com.example.ssmlab2.dao.impl;

import com.example.ssmlab2.dao.GenericDao;
import com.example.ssmlab2.model.User;
import com.mysql.jdbc.Driver;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

//只实现用户注册
public class GenericDaoJDBCImpl<T extends Serializable, ID extends Number> implements GenericDao<T, ID> {

    @Override
    public void create(T t) {
        System.out.println("JDBC");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            new Driver();
            connection = DriverManager.getConnection("jdbc:mysql:///?serverTimezone=UTC", "root", "root");
            String sql = "insert into ssm.user values (null,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            User user = (User) t;
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public List<T> queryAll() {
        return null;
    }

    @Override
    public T queryById(ID id) {
        return null;
    }

    @Override
    public void modify(T t) {

    }

    @Override
    public void remove(ID id) {

    }

    @Override
    public void create(String sqlId, Object parameter) {

    }

    @Override
    public T queryOne(String sqlId, Object parameter) {
        return null;
    }

    @Override
    public void modify(String sqlId, Object parameter) {

    }

    @Override
    public List<T> queryList(String sqlId, Object parameter) {
        return null;
    }

    @Override
    public Object query(String sqlId, Object parameter) {
        return null;
    }
}
